package com.spring.sunflower.userinfo;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ToastUtils;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseCompatActivity;
import com.spring.sunflower.common.TagSelectActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.util.DisplayUtils;
import com.spring.sunflower.util.AppUtils;

/**
 * Created by Kevin on 2021/6/3<br/>
 * <p>
 * Describe:<br/>
 */
public class GenderSelectActivity extends BaseCompatActivity implements View.OnTouchListener {
    private RelativeLayout rlBg, rlContainer;
    private RadioGroup rgGenderSelect;
    private RadioButton rbMale, rbFemale;
    private TextView tvSelectTips, tvNextStep;
    private ImageView ivMale, ivFemale;
    private String genderType; //性别类型
    private int drawableWidth;
    private int displayWidth;
    private float lastX;
    private float currentX;
    private float touchSlop;

    @Override
    public int getLayoutID() {
        return R.layout.activity_gender_select;
    }

    @Override
    public void initView() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        rlBg = findViewById(R.id.bg_layout);
        rgGenderSelect = findViewById(R.id.rgGenderSelect);
        rbMale = findViewById(R.id.rbMale);
        rbFemale = findViewById(R.id.rbFemale);
        ivMale = findViewById(R.id.ivMale);
        ivFemale = findViewById(R.id.ivFemale);
        tvSelectTips = findViewById(R.id.tvTip);
        tvNextStep = findViewById(R.id.tvNextStep);
        rlContainer = findViewById(R.id.rlContainer);
        rgGenderSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbMale) {
                    genderType = Constant.GENDER_MALE;
                    rlBg.setBackgroundResource(R.color.colorPrimary);
                    startMaleSelectAnimation();
                    tvNextStep.setTextColor(ContextCompat.getColor(GenderSelectActivity.this, R.color.colorPrimary));
                    tvSelectTips.setText(R.string.tip_gender_select_male);
                } else {
                    genderType = Constant.GENDER_FEMALE;
                    rlBg.setBackgroundResource(R.color.red_1);
                    ToastUtils.showShort("谨慎选择，拒绝虚假资料");
                    startFemaleSelectAnimation();
                    tvNextStep.setTextColor(ContextCompat.getColor(GenderSelectActivity.this, R.color.red_1));
                    tvSelectTips.setText(R.string.tip_gender_select_female);
                }
            }
        });
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_gender_select_male);
        drawableWidth = drawable.getMinimumWidth();
        displayWidth = AppUtils.getDisplayWidth(this);
        touchSlop = DisplayUtils.dip2px(this, 50);
        RelativeLayout.LayoutParams maleLayoutParams = (RelativeLayout.LayoutParams) ivMale.getLayoutParams();
        maleLayoutParams.setMargins(-drawableWidth / 2, 0, 0, 0);
        ivMale.setLayoutParams(maleLayoutParams);
        RelativeLayout.LayoutParams femaleLayoutParams = (RelativeLayout.LayoutParams) ivFemale.getLayoutParams();
        femaleLayoutParams.setMargins(0, 0, -drawableWidth / 2, 0);
        ivFemale.setLayoutParams(femaleLayoutParams);
//        ivMale.setOnTouchListener(this);
//        ivFemale.setOnTouchListener(this);
        rlContainer.setOnTouchListener(this::onTouch);
        tvNextStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(GenderSelectActivity.this, HobbyActivity.class);
                Intent intent = new Intent(GenderSelectActivity.this, TagSelectActivity.class);
                intent.putExtra(Constant.KEY_GENDER_TYPE, genderType);
                startActivity(intent);
            }
        });
    }

    private void startMaleSelectAnimation() {
        startLeftInAnimation();
        if (ivFemale.getTranslationX() != 0) {
            startRightOutAnimation();
        } else {
            ivFemale.animate()
                    .translationX(drawableWidth / 5)
                    .setInterpolator(new DecelerateInterpolator())
                    .setDuration(300).setStartDelay(500)
                    .start();
        }
    }


    private void startFemaleSelectAnimation() {
        startRightInAnimation();
        if (ivMale.getTranslationX() != 0) {
            startLeftOutAnimation();
        } else {
            ivMale.animate()
                    .translationX(-drawableWidth / 5)
                    .setInterpolator(new DecelerateInterpolator())
                    .setDuration(300).setStartDelay(500)
                    .start();
        }
    }

    private void startLeftInAnimation() {
        if (0 == ivMale.getTranslationX()) {
            startBottomInAnimator();
        }
        ivMale.animate().translationX(displayWidth / 2)
                .alpha(1.0f)
                .setInterpolator(new DecelerateInterpolator())
                .setStartDelay(0)
                .setDuration(500)
                .start();
    }

    private void startRightInAnimation() {

        if (0 == ivFemale.getTranslationX()) {
            startBottomInAnimator();
        }
        ivFemale.animate()
                .translationX(-displayWidth / 2).alpha(1.0f)
                .setInterpolator(new DecelerateInterpolator())
                .setStartDelay(0)
                .setDuration(500)
                .start();
    }

    private void startLeftOutAnimation() {
        ivMale.animate()
                .translationX(-drawableWidth / 5).alpha(0.5f)
                .setInterpolator(new DecelerateInterpolator())
                .setDuration(600)
                .setStartDelay(0)
                .start();
    }

    private void startRightOutAnimation() {
        ivFemale.animate()
                .translationX(drawableWidth / 5).alpha(0.5f)
                .setInterpolator(new DecelerateInterpolator())
                .setDuration(600)
                .setStartDelay(0)
                .start();
    }


    private void startBottomInAnimator() {
        tvNextStep.animate()
                .translationY(DisplayUtils.dip2px(this, -(46 + 12)))
                .setInterpolator(new DecelerateInterpolator())
                .setDuration(500)
                .setStartDelay(0)
                .start();

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: //手指按下
                lastX = event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE: //手指移动（从手指按下到抬起 move多次执行）
                currentX = event.getRawX();
                break;
            case MotionEvent.ACTION_UP: //手指抬起
            case MotionEvent.ACTION_CANCEL:
                if (currentX - lastX > touchSlop) {
                    if (!rbMale.isChecked()) {
                        rbMale.setChecked(true);
                    }
                } else if (currentX - lastX < -touchSlop) {
                    if (!rbFemale.isChecked()) {
                        rbFemale.setChecked(true);
                    }
                }
                break;
        }
        return true;
    }
}
